package com.stlcctv.POSSender;

public class ByteArrayUtil 
{
	public static int byteArrayToInt(byte[] b,int offset)
	{
		int value = 0;
		value += (b[offset]   & 0x000000FF) << 24;
		value += (b[offset+1] & 0x000000FF) << 16;
		value += (b[offset+2] & 0x000000FF) << 8;
		value += (b[offset+3] & 0x000000FF);
		return value;
	}
	
	public static int reverseByteArrayToInt(byte[] b,int offset)
	{
		int value = 0;
		value += (b[offset+3]   & 0x000000FF) << 24;
		value += (b[offset+2] & 0x000000FF) << 16;
		value += (b[offset+1] & 0x000000FF) << 8;
		value += (b[offset] & 0x000000FF);
		return value;
	}
	
	public static void IntToByte(byte[] b,int offset, int number)
	{
		b[offset] 		= (byte) ((number & 0xFF000000) >>> 24);
		b[offset + 1] 	= (byte) ((number & 0x00FF0000) >>> 16);
		b[offset + 2] 	= (byte) ((number & 0x0000FF00) >>> 8);
		b[offset + 3] 	= (byte) (number & 0x000000FF);
	}
	
	public static short byteArrayToShort(byte[] b,int offset)
	{
		short value = 0;
		value += (b[offset]   & 0x00FF) << 8;
		value += (b[offset+1] & 0x00FF);
		return value;
	}
	public static short reverseByteArrayToShort(byte[] b,int offset)
	{
		short value = 0;
		value += (b[offset+1] & 0x00FF) << 8;
		value += (b[offset] & 0x00FF);
		return value;
	}
	public static void ShortToByte(byte[] b,int offset,short number)
	{
		b[offset] 		= (byte) ((number & 0x0000FF00) >>> 8);
		b[offset + 1] 	= (byte)  (number & 0x000000FF);
	}
	
	public static void StringToByte(byte[] b,int offset,String str)
	{
		int str_len = str.length();
		for(int i = 0 ; i < str_len; i++)
			b[offset + i] = (byte) (str.charAt(i));
	}
	
	public static byte[] reverseOrder8(byte src[], int idx){
		byte b[] = new byte[8];
		for(int i=0; i<8; i++){
			b[i]=src[idx+(7-i)];
		}
		return b;
	}
	
	public static byte[] reverseOrder4(byte src[], int idx){
		byte b[] = new byte[4];
		for(int i=0; i<4; i++){
			b[i]=src[idx+(3-i)];
		}
		return b;
	}
	
	public static byte[] reverseOrder2(byte src[], int idx){
		byte b[] = new byte[2];
		for(int i=0; i<2; i++){
			b[i]=src[idx+(1-i)];
		}
		return b;
	}
}
